import { acceptHMRUpdate, defineStore } from 'pinia'
import { ITheme } from '@/store/theme/interface'
import cache from '@/utils/cache'
import { getUploadToken } from '@/api/api'

export const appStore = defineStore('appStore', {
	state: () => ({
		// sidebar 是否展开
		sidebarOpened: cache.getSidebarOpened(),
		// 国际化
		language: cache.getLanguage(),
		// 组件大小
		componentSize: cache.getComponentSize(),
		// 主题
		theme: cache.getTheme(),
		// 阿里云直传参数
		oss:{},
		// 网页可见高度
		clientHeight: cache.getBodyHeight()
	}),
	actions: {
		setSidebarOpened() {
			this.sidebarOpened = !this.sidebarOpened
			cache.setSidebarOpened(this.sidebarOpened)
		},
		setLanguage(locale: string) {
			this.language = locale
			cache.setLanguage(locale)
		},
		setComponentSize(size: string) {
			this.componentSize = size
			cache.setComponentSize(size)
			this.clientHeight = cache.getBodyHeight()
		},
		setTheme(theme: ITheme) {
			this.theme = theme
			cache.setTheme(theme)
		},
		async setOss() {
			const data: any = await getUploadToken()
			this.oss = data.data
			return this.oss
		}
	}
})
// 确保传递正确的 store 声明，本例中为 `useAuth`
if (import.meta.hot) {
    import.meta.hot.accept(acceptHMRUpdate(appStore, import.meta.hot))
}
